package code.jam.activity;

import java.util.List;
import my.andoird.util.r1.JamThread;
import code.jam.R;
import code.jam.core.*;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class JamActivitySelectMusic extends ListActivity
{
	public static final String	DEBUG_TAG		= "JamActivitySelectMusic";

	private JamBGMManager		bgm_manager		= null;
	private PhotoAdapter		photo_adapter	= null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.d(DEBUG_TAG, "onCreate()");
		setContentView(R.layout.main);

		this.bgm_manager = JamManager.getInstance().getBGMMangerInstance();
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setTitle( "Select Music to play");
		
		Handler handler = new Handler();
		handler.post(new Runnable() 
		{
			public void run() 
			{
				while(true)
				{
					if(bgm_manager.isLoaded() == true)
					{
						initializeBGMListView();
						break;
					}
					JamThread.sleep(500);
				}
			}
		});
	}
	
	@Override
	protected void onStart()
	{
		Log.d(DEBUG_TAG, "onStart()");
		super.onStart();
	}

	@Override
	protected void onResume()
	{
		Log.d(DEBUG_TAG, "onResume()");
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		Log.d(DEBUG_TAG, "onPause()");
		super.onPause();
	}

	@Override
	protected void onRestart()
	{
		Log.d(DEBUG_TAG, "onRestart()");
		super.onRestart();
	}

	@Override
	protected void onStop()
	{
		Log.d(DEBUG_TAG, "onStop()");
		super.onStop();
	}

	@Override
	protected void onDestroy()
	{
		Log.d(DEBUG_TAG, "onDestroy()");
		super.onDestroy();
	}

	private void initializeBGMListView()
	{
		getListView().setEmptyView(findViewById(R.id.empty));

		List<String> titles = this.bgm_manager.getTitles();

		String[] str = titles.toArray(new String[titles.size()]);

		this.photo_adapter = new PhotoAdapter(this, str);

		setListAdapter(this.photo_adapter);

		getListView().setTextFilterEnabled(true);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		ListAdapter adapter = l.getAdapter();

		String title = (String) adapter.getItem(position);

		bgm_manager.setCurrentBgmContext(title);

		Intent i = new Intent();

		i.setClass(this, code.jam.activity.JamActivitySelectInstrument.class);

		startActivity(i);

		super.onListItemClick(l, v, position, id);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0, Menu.FIRST + 1, 0, "show");
		menu.add(0, Menu.FIRST + 2, 0, "client");
		menu.add(0, Menu.FIRST + 3, 0, "record to stop");

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case Menu.FIRST + 1:
			{
				break;
			}
			case Menu.FIRST + 2:
			{
				break;
			}

			case Menu.FIRST + 3:
			{
				break;
			}
		}
		
		return super.onOptionsItemSelected(item);
	}

	private static class PhotoAdapter extends BaseAdapter
	{
		private Bitmap			icon		= null;
		private String[]		bgm_name	= null;
		private LayoutInflater	inflater	= null;

		public PhotoAdapter(Context context, String[] bgm_name)
		{
			this.inflater = LayoutInflater.from(context);
			this.bgm_name = bgm_name;

			this.icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.bgm_icon);
		}

		public int getCount()
		{
			return this.bgm_name.length;
		}

		public Object getItem(int arg0)
		{
			return this.bgm_name[arg0];
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder holder = null;

			if (convertView == null)
			{
				convertView = this.inflater.inflate(R.layout.list_item_icon_text, null);

				holder = new ViewHolder();
				holder.text = (TextView) convertView.findViewById(R.id.text);
				holder.icon = (ImageView) convertView.findViewById(R.id.icon);

				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}

			holder.text.setText(this.bgm_name[position]);
			holder.text.setTextSize( 17); 
			holder.icon.setImageBitmap(this.icon);

			return convertView;
		}

		static class ViewHolder
		{
			TextView	text;
			ImageView	icon;
		}
	}
}	
